package com.yqy.wordvideodemo.ui;

import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.util.Log;
import android.widget.FrameLayout;
import com.shizhefei.fragment.LazyFragment;
import com.tencent.smtt.sdk.TbsReaderView;
import com.yqy.wordvideodemo.R;

import java.io.File;
import java.util.Objects;

/**
 * 清单文件 application里 要加 requestLegacyExternalStorage = true 要不android Q会出问题
 * <p>
 * usesCleartextTraffic = true 也要加 android 9 才能允许http请求
 */
public class WordFragment extends LazyFragment {

    private FrameLayout container;
    private TbsReaderView readerView;
    private String fileName;
    private  File file;

    public WordFragment(String s){
        fileName = s;
    }
    @Override
    protected void onCreateViewLazy(Bundle savedInstanceState) {
        super.onCreateViewLazy(savedInstanceState);
        setContentView(R.layout.fragment_word);
        container = findViewById(R.id.container);

        file = new File(Environment.getExternalStorageDirectory() + "/texts" + "/"+fileName);

//        openFile(file.getAbsolutePath());
    }

    @Override
    protected void onDestroyViewLazy() {
        super.onDestroyViewLazy();
    }

    @Override
    protected void onFragmentStopLazy() {
        super.onFragmentStopLazy();
        //退出时一定要调用这个方法，否则下次加载不会成功
        if (readerView != null) {
            readerView.onStop();
            container.removeView(readerView);
            readerView = null;
        }
    }

    @Override
    protected void onFragmentStartLazy() {
        super.onFragmentStartLazy();
        Log.d("第一次加载","第一次加载"+fileName);
        openFile(file.getAbsolutePath());
    }

    private void openFile(String path) {
        if (getActivity() == null) return;

        if (readerView == null){
            readerView = new TbsReaderView(getActivity(), new TbsReaderView.ReaderCallback() {
                @Override
                public void onCallBackAction(Integer integer, Object o, Object o1) {

                }
            });
        }
        // 往容器里添加TbsReaderView控件
        container.addView(readerView);

        //增加下面一句解决没有TbsReaderTemp文件夹存在导致加载文件失败
        //Android 10 不允许在外置存储 根目录建立文件夹 只能用getExternalFilesDir() 创建android/data/包名/files
        String bsReaderTemp = Objects.requireNonNull(getActivity().getExternalFilesDir(null)).getAbsolutePath();
        File bsReaderTempFile = new File(bsReaderTemp + "/TbsReaderTemp");
        if (!bsReaderTempFile.exists()) {
            Log.d("print", "准备创建/TbsReaderTemp！！");
            boolean mkdir = bsReaderTempFile.mkdir();
            if (!mkdir) {
                Log.d("print", "创建/TbsReaderTemp失败！！！！！");
            }
        }

        //通过bundle把文件传给x5,打开的事情交由x5处理
        Bundle bundle = new Bundle();
        //传递文件路径
        bundle.putString("filePath", path);
        //临时的路径
        bundle.putString("tempPath", bsReaderTempFile.getAbsolutePath());

        //加载文件前的初始化工作,加载支持不同格式的插件
        boolean b = readerView.preOpen(getFileType(path), false);
        if (b) {
            readerView.openFile(bundle);
        }


    }

    /***
     * 获取文件类型
     *
     * @param path 文件路径
     * @return 文件的格式
     */
    private String getFileType(String path) {
        String str = "";

        if (TextUtils.isEmpty(path)) {
            return str;
        }
        int i = path.lastIndexOf('.');
        if (i <= -1) {
            return str;
        }
        str = path.substring(i + 1);
        return str;
    }
}
